package com.i2bt.app.main;

import com.i2bt.common.Contract;
import com.i2bt.common.UserConfig;

import com.i2bt.R;
import com.i2bt.adapter.MainPagerAdapter;
import com.i2bt.app.BaseActivity;
import com.i2bt.app.BaseFragment;
import com.i2bt.app.files.FilesFragment;
import com.i2bt.app.friend.FriendAddActivity;
import com.i2bt.app.friend.FriendFragment;
import com.i2bt.app.friend.FriendMessageActivity;
import com.i2bt.app.main.model.HeartBeatHandle;
import com.i2bt.app.set.SetActivity;
import com.i2bt.app.share.ShareFragment;
import com.i2bt.app.transmission.TransmissionFragment;
import com.i2bt.bean.FriendBean;
import com.i2bt.util.FriendMessageUtil;

import android.annotation.SuppressLint;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.viewpagerindicator.TabPageIndicator;

import java.util.List;

public class MainActivity extends BaseActivity {

	private ViewPager mainPager;

	private TabPageIndicator mainTabIndicator;

	private HeartBeatHandle heartBeatJsonHandle;
//	private ShareFragment sharePage = new ShareFragment();
	public static TransmissionFragment transmission = new TransmissionFragment();
	private BaseFragment[] mainFragments = new BaseFragment[] {
			new FilesFragment(), new ShareFragment(), new FriendFragment(), transmission };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		logic();
		initView();
	}

	@Override
	protected void logic() {
		// 开启心跳
		if (heartBeatJsonHandle == null) {
			heartBeatJsonHandle = new HeartBeatHandle(this);
			heartBeatJsonHandle.start();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@SuppressLint("NewApi")
	@Override
	protected void initView() {
		if (Build.VERSION.SDK_INT < 11) {
			actionBarSupport.show();
		}

		mainPager = (ViewPager) findViewById(R.id.main_viewPager);
		MainPagerAdapter pagerAdapter = new MainPagerAdapter(
				getSupportFragmentManager(), mainFragments);
		mainPager.setAdapter(pagerAdapter);

		mainTabIndicator = (TabPageIndicator) findViewById(R.id.main_tabs);
		mainTabIndicator.setViewPager(mainPager, 0);

		mainTabIndicator.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageSelected(int arg0) {
				// TODO Auto-generated method stub
				if (arg0 == 1) {
					((FilesFragment) mainFragments[0]).changeType(false);
					setActionBarTitle(Contract.TITLE_APP_NAME);
				}
			}

		});
	}

	/**
	 * 弹出消息到消息栏
	 */
	public void notifyMessage(List<FriendBean> friendMessageList) {
		PendingIntent messagePeIntent = PendingIntent.getActivity(this, 0,
				new Intent(this, FriendMessageActivity.class)
						.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT), 0);

		// 创建同意信息栏通知
		NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
				getApplication());
		notificationBuilder.setWhen(System.currentTimeMillis())
				.setSmallIcon(R.drawable.logo_ico)
				.setContentIntent(messagePeIntent).setAutoCancel(true);

		if (friendMessageList.size() == 1) {
			// 只有1条信息过来就显示好友的昵称发来一条信息
			FriendBean friendBean = friendMessageList.get(0);
			String friendName = "";
			if (friendBean.getSender().equals(UserConfig.userName)) {
				friendName = friendBean.getReceiver();
			} else {
				friendName = friendBean.getSender();
			}
			FriendMessageUtil.messageContentDecoded(friendBean);
			notificationBuilder.setTicker(friendName + "发来一条信息")
					.setContentText(friendMessageList.get(0).getContent())
					.setContentTitle(friendName);
		} else {
			notificationBuilder
					.setTicker("您有" + friendMessageList.size() + "个消息未处理")
					.setContentText("您有" + friendMessageList.size() + "个消息未处理")
					.setContentTitle(Contract.TITLE_APP_NAME);
		}

		// 显示通知信息
		NotificationManager notiManage = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		notiManage.cancelAll();
		notiManage.notify(0, notificationBuilder.build());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		boolean result = super.onCreateOptionsMenu(menu);
		getMenuInflater().inflate(R.menu.main_menu, menu);
		return result;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {

		case android.R.id.home:
			int CurrentPager = mainPager.getCurrentItem();
			mainFragments[CurrentPager].backHomeOperate();
			break;

		case R.id.action_settings:
			goActivity(SetActivity.class);
			break;

		case R.id.action_message:
			goActivity(FriendMessageActivity.class);
			break;

		case R.id.action_add_friend:
			goActivity(FriendAddActivity.class);
			break;
		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		boolean result = false;
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK: {
			result = mainFragments[mainPager.getCurrentItem()].onKeyBack();
			break;
		}
		}
		if (result) {
			return result;
		} else {
			switch (keyCode) {

			case KeyEvent.KEYCODE_BACK: {
				finish();
				return false;
			}

			}
			return super.onKeyDown(keyCode, event);
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		// 注销心跳
		heartBeatJsonHandle.close();
		System.exit(0);
	}

	@Override
	public void onClick(View v) {
	}

	@Override
	public void backHomeOperate() {

	}

}
